﻿// code03040200.cpp : 迭代器
// 利用迭代器进行二分搜索

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v{ 1,3,5,7,9,11,13,15,17,19 };
    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
    cout << endl;
    cout << "输入想要查找的值：" << endl;
    int find;
    cin >> find;

    auto beg = v.cbegin();
    auto end = v.cend();
    auto mid = beg + (end-beg)/ 2;
    
    while (mid!=end&& *mid!=find)
    {
	    if (*mid>find)
	    {
            end = mid;
	    }
        else
        {
            beg =mid+1;
        }
        mid = beg + (end - beg) / 2;
	    
    }
    if (mid==end)
    {
        cout << "查无此数！" << endl;
    }
    else
    {
        cout << find << "是数组中第" << mid - v.begin() + 1 << "个元素" << endl;
    }    

    return 0;
}


